package cn.e3.sso.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.e3.sso.service.LoginService;
import cn.e3.utils.CookieUtils;
import cn.e3.utils.E3Result;

@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;
	@RequestMapping("/page/login")
	public String login(String redirect,Model model){
		model.addAttribute("redirect", redirect);
		return "login";
	}
	@RequestMapping(value="/user/login",method=RequestMethod.POST)
	@ResponseBody
	public E3Result loginToken(String username,String password,HttpServletRequest request,
			HttpServletResponse response){
		E3Result e3Result = loginService.login(username, password);
		//判断是否登陆成功
		if(e3Result.getStatus()==200){
			//取出token
			String token = e3Result.getData().toString();
			//将token写到cookie中
			CookieUtils.setCookie(request, response, "token", token);
		}
		return e3Result;
	}
}
